<?php
	require_once("dbconnect.php");
	require_once("ekgames.php");
	
	ob_end_clean(); //Clear all choed & printed item
	
//Receive then respond the request
	$requestFunction = receiveRequest("action");

	switch ($requestFunction) {
		case "login":
			echo login();
			break;
		
		default:
			echo "ERR#01: Bad request!";
	}
	
	mysql_close($con);
	
	//=======================================================================================
	
	function login () {
		
		$userid = receiveRequest("userid");
		$pass = receiveRequest("pass");

		if($userid == null or $pass == null) {
			badRequest();
			return;
		}
		
		$sql = "SELECT * FROM users WHERE ID='$userid' AND PASS='$pass'";
		$q	 = mysql_query($sql) or die(mysql_error());
		$count = mysql_num_rows($q);
		
		$xml = "<root>";
		$xml .= "<login>";
		$xml .= "<success>$count</success>";  
		
		if($count > 0) {
			$xml .= loadUser();
		}
		
		$xml .= "</login>";
		$xml .= "</root>";
				
		return $xml;
		
	}

	function loadUser () {
		$userid = receiveRequest("userid");
		
		if($userid == null) {
			badRequest();
			return;
		}
		
		$sql = "SELECT * FROM users WHERE ID='$userid'";
		$q	 = mysql_query($sql) or die(mysql_error());
		
		$xml = "";
		while($r = mysql_fetch_array($q)){
			$xml .= "<NUM>".$r['NUM']."</NUM>";  
			$xml .= "<ID>".$r['ID']."</ID>";
			$xml .= "<CNAME>".$r['CNAME']."</CNAME>";  
			$xml .= "<URACE>".$r['URACE']."</URACE>";    
			$xml .= "<MAINCARD>".$r['MAINCARD']."</MAINCARD>";
			$xml .= "<SKILL_POINT>".$r['SKILL_POINT']."</SKILL_POINT>";
			$xml .= "<UHP>".$r['UHP']."</UHP>";  
			$xml .= "<UMANA>".$r['UMANA']."</UMANA>";
			$xml .= "<EXP>".$r['EXP']."</EXP>";  
			$xml .= "<Level>".$r['Level']."</Level>";    
			$xml .= "<READY>".$r['READY']."</READY>";
		}
		
		return $xml;
	}

?>